/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKPanels;

import java.awt.Dimension;
import java.io.IOException;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;

import jktest.JKMainPanel;
import jktest.JKMainGui;
import jktest.JMultilineLabel;
import jktest.JKButton;

/**
 * 
 * Panel Class containing all elements of the Floppy Test
 * 
 * @author Royce Feng
 */

public class FloppyPanel extends JKMainPanel {

    private PassFailPanel passFail;
    private JTextArea textArea;
    
    /**
     * 
     * Main Constructor for FloppyPanel. Creates a multiline label with instructions
     * as well as the three main buttons: Start test, Pass, Fail.
     * 
     * @param _parent The Parent Class (for communication)
     */
    public FloppyPanel ( JKMainGui _parent ) {
        super(_parent);
        JMultilineLabel instructions = new JMultilineLabel();
        instructions.setMaxWidth(500);
        instructions.setText("After you click this button, JKTest will automatically start the test.");
        this.add(instructions);
        JKButton button = new JKButton(this,"Floppy");
        button.setPreferredSize(new Dimension(200,25));
        this.add( button );
        textArea = new JTextArea(5,40);
        this.add(textArea);
        passFail = new PassFailPanel(this);
        passFail.setPreferredSize(new Dimension(500,25));
        this.add(passFail);
    }
    
    /**
     * 
     * Returns the name of the panel
     * 
     * @return The name of the panel
     */
    @Override
    public String getName() {
        return "Floppy";
    }

    /**
     *
     * Private threading class to run the floppy test in a seperate thread
     *
     */
    private class FloppyRunner implements Runnable  {

        private FloppyPanel parent;

        public FloppyRunner ( FloppyPanel _parent ) {
            parent = _parent;
        }

        /**
        *
        * The Floppy test method
        * 
        */
        public void run() {
            /*
            if ( getThisParent().getMessageState() )
                JOptionPane.showMessageDialog(null, "Floppy Test is an automatic procedure. If you wish to verify this " +
                        "manually, please open up a Windows Explorer window and access the floppy through that.",
                        "Automated Procedure",JOptionPane.INFORMATION_MESSAGE);
            */
            try {
                String directory = "A:";
                textArea.append("Accessing Floppy Drive...");
                boolean exists = (new File(directory)).exists();
                if (!exists) {
                    directory = JOptionPane.showInputDialog("JKTest could not find the floppy drive at A:, please specify the drive letter: ","A:");
                    if ( directory.equals("A:") || ( !(new File(directory)).exists() ) ) {
                        textArea.append("FAILED\n");
                        parent.getThisParent().valChanged(parent.getName(), false );
                        return;
                    }
                }
                textArea.append("PASSED\nCreating new File...");
                File f = new File(directory + "\\JKTestFile.txt");
                if (!f.createNewFile()) {
                    textArea.append("FAILED\n");
                    parent.getThisParent().valChanged(parent.getName(), false);
                } else {
                    textArea.append("PASSED\nWriting to File...");
                    BufferedWriter out = new BufferedWriter(new FileWriter(f));
                    out.write("aString");
                    out.close();
                    textArea.append("PASSED\nDeleting file...");
                    if (!f.delete()) {
                        textArea.append("FAILED\n");
                        parent.getThisParent().valChanged(parent.getName(), false);
                    } else {
                        textArea.append("PASSED\n");
                        parent.getThisParent().valChanged(parent.getName(), true);
                    }
                }
            } catch ( IOException e ) {
                textArea.append("FAILED\n");
                parent.getThisParent().valChanged(parent.getName(), false );
            }
        }
    }
    
    /**
     * 
     * Handles button clicks. Pass/fail gets sent to parent while the test is run
     * when the test button is clicked.
     * 
     * @param source Button source (usually the button text)
     */
    public void ButtonClicked( String source ) {
        if ( source.equals("Pass") ) {
            getThisParent().valChanged(this.getName(), true);
            getThisParent().incPanel();
        } else if ( source.equals("Fail") ) {
            getThisParent().valChanged(this.getName(), false);
            getThisParent().incPanel();
        } else {
            Thread t = new Thread( new FloppyRunner(this) );
            t.start();
        }
    }    
    
}
